package org.xg.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

/**
 * @author CMG
 * @version 1.0
 * @className DoServlet
 * @description
 * @date 2021/9/23 22:06
 **/
@WebServlet("/DoServlet")
public class DoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter printWriter = resp.getWriter();
        String webName = (String) req.getAttribute("webName");
        String url = (String) req.getAttribute("url");
        String welcome = (String) req.getAttribute("welcome");
        if (null != webName) {
            printWriter.write("<h3>" + webName + "</h3>");
        }
        if (null != url) {
            printWriter.write("<h3>" + url + "</h3>");
        }
        if (null != welcome) {
            printWriter.write("<h3>" + welcome + "</h3>");
        }
        String username = req.getParameter("username");
        // 获取密码
        String password = req.getParameter("password");
        // 获取性别
        String sex = req.getParameter("sex");
        // 获取城市
        String city = req.getParameter("city");
        // 获取使用语言返回是String数组
        String[] languages = req.getParameterValues("language");
        printWriter.write("用户名：" + username + "<br/>" + "密码：" + password + "<br/>" + "性别：" + sex + "<br/>" + "城市：" + city
                + "<br/>" + "使用过的语言：" + Arrays.toString(languages) + "<br/>"
        );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
